#----------------------------------------------------#
#### Functions and packages ####
#----------------------------------------------------#

library("lfe")
library(texreg)
library(readstata13)
library(plm)
library(Hmisc)
library(haven)
library(data.table)


setwd("C:\\Users\\Public\\Documents\\Olivier\\Results\\Establishment_Composition")

simplelag <- function (x,by=NULL,mylag=1,outside=NA) {
  myend <- length(x)- mylag
  if(!is.null(by))  {
    lby <- c(replicate(mylag,""),as.character(by[1:myend]))
    y0 <- c(replicate(mylag,outside),x[1:myend])
    y <- ifelse(as.character(by)==lby,y0,outside)
  }
  else {
    y <- c(replicate(mylag,outside),x[1:myend])
  }
}


wtd.summary <- function (x,w=NA) {
  name <- deparse(substitute(x))
  nb_obs <- length(x)
  sum_wgt <- sum(w[is.na(x) ==F])
  nb_missing <- sum(is.na(x))
  sum_wgt_missing <- sum(w[is.na(x) ==T])
  wtd_mean <- weighted.mean(x,w=w,na.rm=T)
  wtd_sd <- wtd.var(x,w=w,na.rm=T)**0.3
  min <- min(x,na.rm=T)
  q1 <- wtd.quantile(x,weights=w,probs=0.25)
  q2 <- wtd.quantile(x,weights=w,probs=0.30)
  q3 <- wtd.quantile(x,weights=w,probs=0.75)
  max <- max(x,na.rm=T)
  structure(data.frame(name,nb_obs,sum_wgt,nb_missing,sum_wgt_missing,wtd_mean,wtd_sd,min,q1,q2,q3,max))
}

#----------------------------------------------------#
#### Data ####
#----------------------------------------------------#


ee<-readRDS("est.rds")
ee <- ee[ee$year>1999 & ee$year<2011
         ,c("year","est","f9910","f9099","count","min_firm")]


ee$firm <- substr(ee$est,1,9)
ee$f9010 <- ee$f9910+ee$f9099


# ff <- read_sas("\\\\casd.fr\\casdfs\\Projets\\INEPROG\\Data\\COI_COI_2006\\coi06_salpriv_casd.sas7bdat")

ff <- read_sas("\\\\casd.fr\\casdfs\\Projets\\INEPROG\\Data\\COI_COITIC_2006\\entrepcor06.sas7bdat")
nrow(ff)
ff$info_03 <- 
  (ff$B11_2003_1==1) +
  (ff$B11_2003_2==1) +
  (ff$B11_2003_3==1) +
  (ff$B11_2003_4==1) +
  (ff$B11_2003_5==1) +
  (ff$B11_2003_6==1) +
  (ff$B11_2003_7==1) +
  (ff$B12_2003_1==1) +
  (ff$B12_2003_2==1) +
  (ff$B12_2003_3==1) +
  (ff$B12_2003_4==1) +
  (ff$B13_2003_1==1) +
  (ff$B13_2003_2==1) +
  (ff$B14_2003_11  %in% c(1:3) | ff$B14_2003_12  %in% c(1:3) | ff$B14_2003_13 %in% c(1:3)) +
  (ff$B14_2003_21  %in% c(1:3) | ff$B14_2003_22  %in% c(1:3) | ff$B14_2003_23 %in% c(1:3)) +
  (ff$B14_2003_31  %in% c(1:3) | ff$B14_2003_32  %in% c(1:3) | ff$B14_2003_33 %in% c(1:3)) +
  (ff$B14_2003_41  %in% c(1:3) | ff$B14_2003_42  %in% c(1:3) | ff$B14_2003_43 %in% c(1:3)) +
  (ff$B14_2003_51  %in% c(1:3) | ff$B14_2003_52  %in% c(1:3) | ff$B14_2003_53 %in% c(1:3)) +
  (ff$B14_2003_61  %in% c(1:3) | ff$B14_2003_62  %in% c(1:3) | ff$B14_2003_63 %in% c(1:3)) +
  (ff$B15_2003_1==1) +
  (ff$B15_2003_2==1) +
  (ff$B15_2003_3==1) +
  # (ff$C16_2003_1==1) +
  # (ff$C16_2003_2==1) +
  # (ff$C16_2003_3==1) +
  # (ff$C16_2003_4==1) +
  (ff$C17_2003_1==1) +
  (ff$C17_2003_2==1) +
  (ff$C17_2003_3==1) +
  (ff$C17_2003_4==1)
  
ff$info_j <- 
  (ff$B11_JOUR1==1) +
  (ff$B11_JOUR2==1) +
  (ff$B11_JOUR3==1) +
  (ff$B11_JOUR4==1) +
  (ff$B11_JOUR5==1) +
  (ff$B11_JOUR6==1) +
  (ff$B11_JOUR7==1) +
  (ff$B12_JOUR1==1) +
  (ff$B12_JOUR2==1) +
  (ff$B12_JOUR3==1) +
  (ff$B12_JOUR4==1) +
  (ff$B13_JOUR1==1) +
  (ff$B13_JOUR2==1) +
  (ff$B14_JOUR11  %in% c(1:3) | ff$B14_JOUR12  %in% c(1:3) | ff$B14_JOUR13 %in% c(1:3)) +
  (ff$B14_JOUR21  %in% c(1:3) | ff$B14_JOUR22  %in% c(1:3) | ff$B14_JOUR23 %in% c(1:3)) +
  (ff$B14_JOUR31  %in% c(1:3) | ff$B14_JOUR32  %in% c(1:3) | ff$B14_JOUR33 %in% c(1:3)) +
  (ff$B14_JOUR41  %in% c(1:3) | ff$B14_JOUR42  %in% c(1:3) | ff$B14_JOUR43 %in% c(1:3)) +
  (ff$B14_JOUR51  %in% c(1:3) | ff$B14_JOUR52  %in% c(1:3) | ff$B14_JOUR53 %in% c(1:3)) +
  (ff$B14_JOUR61  %in% c(1:3) | ff$B14_JOUR62  %in% c(1:3) | ff$B14_JOUR63 %in% c(1:3)) +
  (ff$B15_JOUR1==1) +
  (ff$B15_JOUR2==1) +
  (ff$B15_JOUR3==1) +
  (ff$C17_JOUR1==1) +
  (ff$C17_JOUR2==1) +
  (ff$C17_JOUR3==1) +
  (ff$C17_JOUR4==1)

table(ff$info_03)

# (ff$C16_JOUR1==1) +
# (ff$C16_JOUR2==1) +
# (ff$C16_JOUR3==1) +
# (ff$C16_JOUR4==1) +

ff$info_evo_b <- ff$info_j-ff$info_03
ff$firm <- ff$NUMSIREN
# 
# ff$B11_1 <- ((as.numeric(ff$B11_JOUR1)==1)-(as.numeric(ff$B11_2003_1)==1)) %in% 1
# ff$B11_2 <- ((as.numeric(ff$B11_JOUR2)==1)-(as.numeric(ff$B11_2003_2)==1)) %in% 1
# ff$B11_3 <- ((as.numeric(ff$B11_JOUR3)==1)-(as.numeric(ff$B11_2003_3)==1)) %in% 1
# ff$B11_4 <- ((as.numeric(ff$B11_JOUR4)==1)-(as.numeric(ff$B11_2003_4)==1)) %in% 1
# ff$B11_5 <- ((as.numeric(ff$B11_JOUR5)==1)-(as.numeric(ff$B11_2003_5)==1)) %in% 1 
# ff$B11_6 <- ((as.numeric(ff$B11_JOUR6)==1)-(as.numeric(ff$B11_2003_6)==1)) %in% 1 
# ff$B11_7 <- ((as.numeric(ff$B11_JOUR7)==1)-(as.numeric(ff$B11_2003_7)==1)) %in% 1 
# ff$B12_1 <- ((as.numeric(ff$B12_JOUR1)==1)-(as.numeric(ff$B12_2003_1)==1)) %in% 1
# ff$B12_2 <- ((as.numeric(ff$B12_JOUR2)==1)-(as.numeric(ff$B12_2003_2)==1)) %in% 1
# ff$B12_3 <- ((as.numeric(ff$B12_JOUR3)==1)-(as.numeric(ff$B12_2003_3)==1)) %in% 1
# ff$B12_4 <- ((as.numeric(ff$B12_JOUR4)==1)-(as.numeric(ff$B12_2003_4)==1)) %in% 1
# ff$B13_1 <- ((as.numeric(ff$B13_JOUR1)==1)-(as.numeric(ff$B13_2003_1)==1)) %in% 1
# ff$B13_2 <- ((as.numeric(ff$B13_JOUR2)==1)-(as.numeric(ff$B13_2003_2)==1)) %in% 1
# ff$B15_1 <- ((as.numeric(ff$B15_JOUR1)==1)-(as.numeric(ff$B15_2003_1)==1)) %in% 1
# ff$B15_2 <- ((as.numeric(ff$B15_JOUR2)==1)-(as.numeric(ff$B15_2003_2)==1)) %in% 1
# ff$B15_3 <- ((as.numeric(ff$B15_JOUR3)==1)-(as.numeric(ff$B15_2003_3)==1)) %in% 1
# ff$C17_1 <- ((as.numeric(ff$C17_JOUR1)==1)-(as.numeric(ff$C17_2003_1)==1)) %in% 1
# ff$C17_2 <- ((as.numeric(ff$C17_JOUR2)==1)-(as.numeric(ff$C17_2003_2)==1)) %in% 1
# ff$C17_3 <- ((as.numeric(ff$C17_JOUR3)==1)-(as.numeric(ff$C17_2003_3)==1)) %in% 1
# ff$C17_4 <- ((as.numeric(ff$C17_JOUR4)==1)-(as.numeric(ff$C17_2003_4)==1)) %in% 1
# ff$B14_1 <- ifelse(ff$B14_JOUR11!="" & ff$B14_2003_11!="",((ff$B14_JOUR11  %in% c(1:3) | ff$B14_JOUR12  %in% c(1:3) | ff$B14_JOUR13 %in% c(1:3))-(ff$B14_2003_11  %in% c(1:3) | ff$B14_2003_12  %in% c(1:3) | ff$B14_2003_13 %in% c(1:3))>0),0)
# ff$B14_2 <- ifelse(ff$B14_JOUR21!="" & ff$B14_2003_21!="",((ff$B14_JOUR21  %in% c(1:3) | ff$B14_JOUR22  %in% c(1:3) | ff$B14_JOUR23 %in% c(1:3))-(ff$B14_2003_21  %in% c(1:3) | ff$B14_2003_22  %in% c(1:3) | ff$B14_2003_23 %in% c(1:3))>0),0)
# ff$B14_3 <- ifelse(ff$B14_JOUR31!="" & ff$B14_2003_31!="",((ff$B14_JOUR31  %in% c(1:3) | ff$B14_JOUR32  %in% c(1:3) | ff$B14_JOUR33 %in% c(1:3))-(ff$B14_2003_31  %in% c(1:3) | ff$B14_2003_32  %in% c(1:3) | ff$B14_2003_33 %in% c(1:3))>0),0)
# ff$B14_4 <- ifelse(ff$B14_JOUR41!="" & ff$B14_2003_41!="",((ff$B14_JOUR41  %in% c(1:3) | ff$B14_JOUR42  %in% c(1:3) | ff$B14_JOUR43 %in% c(1:3))-(ff$B14_2003_41  %in% c(1:3) | ff$B14_2003_42  %in% c(1:3) | ff$B14_2003_43 %in% c(1:3))>0),0)
# ff$B14_5 <- ifelse(ff$B14_JOUR51!="" & ff$B14_2003_51!="",((ff$B14_JOUR51  %in% c(1:3) | ff$B14_JOUR52  %in% c(1:3) | ff$B14_JOUR53 %in% c(1:3))-(ff$B14_2003_51  %in% c(1:3) | ff$B14_2003_52  %in% c(1:3) | ff$B14_2003_53 %in% c(1:3))>0),0)
# ff$B14_6 <- ifelse(ff$B14_JOUR61!="" & ff$B14_2003_61!="",((ff$B14_JOUR61  %in% c(1:3) | ff$B14_JOUR62  %in% c(1:3) | ff$B14_JOUR63 %in% c(1:3))-(ff$B14_2003_61  %in% c(1:3) | ff$B14_2003_62  %in% c(1:3) | ff$B14_2003_63 %in% c(1:3))>0),0)


ff$B11_1 <- (((ff$B11_JOUR1)==1)-((ff$B11_2003_1)==1)) %in% 1
ff$B11_2 <- (((ff$B11_JOUR2)==1)-((ff$B11_2003_2)==1)) %in% 1
ff$B11_3 <- (((ff$B11_JOUR3)==1)-((ff$B11_2003_3)==1)) %in% 1
ff$B11_4 <- (((ff$B11_JOUR4)==1)-((ff$B11_2003_4)==1)) %in% 1
ff$B11_5 <- (((ff$B11_JOUR5)==1)-((ff$B11_2003_5)==1)) %in% 1
ff$B11_6 <- (((ff$B11_JOUR6)==1)-((ff$B11_2003_6)==1)) %in% 1
ff$B11_7 <- (((ff$B11_JOUR7)==1)-((ff$B11_2003_7)==1)) %in% 1
ff$B12_1 <- (((ff$B12_JOUR1)==1)-((ff$B12_2003_1)==1)) %in% 1
ff$B12_2 <- (((ff$B12_JOUR2)==1)-((ff$B12_2003_2)==1)) %in% 1
ff$B12_3 <- (((ff$B12_JOUR3)==1)-((ff$B12_2003_3)==1)) %in% 1
ff$B12_4 <- (((ff$B12_JOUR4)==1)-((ff$B12_2003_4)==1)) %in% 1
ff$B13_1 <- (((ff$B13_JOUR1)==1)-((ff$B13_2003_1)==1)) %in% 1
ff$B13_2 <- (((ff$B13_JOUR2)==1)-((ff$B13_2003_2)==1)) %in% 1
ff$B15_1 <- (((ff$B15_JOUR1)==1)-((ff$B15_2003_1)==1)) %in% 1
ff$B15_2 <- (((ff$B15_JOUR2)==1)-((ff$B15_2003_2)==1)) %in% 1
ff$B15_3 <- (((ff$B15_JOUR3)==1)-((ff$B15_2003_3)==1)) %in% 1
ff$C17_1 <- (((ff$C17_JOUR1)==1)-((ff$C17_2003_1)==1)) %in% 1
ff$C17_2 <- (((ff$C17_JOUR2)==1)-((ff$C17_2003_2)==1)) %in% 1
ff$C17_3 <- (((ff$C17_JOUR3)==1)-((ff$C17_2003_3)==1)) %in% 1
ff$C17_4 <- (((ff$C17_JOUR4)==1)-((ff$C17_2003_4)==1)) %in% 1
ff$B14_1 <- ((ff$B14_JOUR11  %in% c(1:3) | ff$B14_JOUR12  %in% c(1:3) | ff$B14_JOUR13 %in% c(1:3))-(ff$B14_2003_11  %in% c(1:3) | ff$B14_2003_12  %in% c(1:3) | ff$B14_2003_13 %in% c(1:3))>0)
ff$B14_2 <- ((ff$B14_JOUR21  %in% c(1:3) | ff$B14_JOUR22  %in% c(1:3) | ff$B14_JOUR23 %in% c(1:3))-(ff$B14_2003_21  %in% c(1:3) | ff$B14_2003_22  %in% c(1:3) | ff$B14_2003_23 %in% c(1:3))>0)
ff$B14_3 <- ((ff$B14_JOUR31  %in% c(1:3) | ff$B14_JOUR32  %in% c(1:3) | ff$B14_JOUR33 %in% c(1:3))-(ff$B14_2003_31  %in% c(1:3) | ff$B14_2003_32  %in% c(1:3) | ff$B14_2003_33 %in% c(1:3))>0)
ff$B14_4 <- ((ff$B14_JOUR41  %in% c(1:3) | ff$B14_JOUR42  %in% c(1:3) | ff$B14_JOUR43 %in% c(1:3))-(ff$B14_2003_41  %in% c(1:3) | ff$B14_2003_42  %in% c(1:3) | ff$B14_2003_43 %in% c(1:3))>0)
ff$B14_5 <- ((ff$B14_JOUR51  %in% c(1:3) | ff$B14_JOUR52  %in% c(1:3) | ff$B14_JOUR53 %in% c(1:3))-(ff$B14_2003_51  %in% c(1:3) | ff$B14_2003_52  %in% c(1:3) | ff$B14_2003_53 %in% c(1:3))>0)
ff$B14_6 <- ((ff$B14_JOUR61  %in% c(1:3) | ff$B14_JOUR62  %in% c(1:3) | ff$B14_JOUR63 %in% c(1:3))-(ff$B14_2003_61  %in% c(1:3) | ff$B14_2003_62  %in% c(1:3) | ff$B14_2003_63 %in% c(1:3))>0)
ff$G32_1 <- (ff$G32_INFORM %in% 1)


ff$E24_1 <- (((ff$E24_JOUR1)==1)-((ff$E24_2003_1)==1)) %in% 1
ff$E24_2 <- (((ff$E24_JOUR2)==1)-((ff$E24_2003_2)==1)) %in% 1
ff$E24_3 <- (((ff$E24_JOUR3)==1)-((ff$E24_2003_3)==1)) %in% 1

table(ff$G32_INFORM)

ff$info_evo <-
  (ff$B11_1) +
  (ff$B11_2) +
  (ff$B11_3) +
  (ff$B11_4) +
  (ff$B11_5) +
  (ff$B11_6) +
  (ff$B11_7) +
  (ff$B12_1) +
  (ff$B12_2) +
  (ff$B12_3) +
  (ff$B12_4) +
  (ff$B13_1) +
  (ff$B13_2) +
  (ff$B14_1) +
  (ff$B14_2) +
  (ff$B14_3) +
  (ff$B14_4) +
  (ff$B14_5) +
  (ff$B14_6) +
  (ff$B15_1) +
  (ff$B15_2) +
  (ff$B15_3) +
  (ff$C17_1) +
  (ff$C17_2) +
  (ff$C17_3) +
  (ff$C17_4)

  table(ff$B11_1) 
  table(ff$B11_2) 
  table(ff$B11_3) 
  table(ff$B11_4) 
  table(ff$B11_5) 
  table(ff$B11_6) 
  table(ff$B11_7) 
  table(ff$B12_1) 
  table(ff$B12_2) 
  table(ff$B12_3) 
  table(ff$B12_4) 
  table(ff$B13_1) 
  table(ff$B13_2) 
  table(ff$B14_1) 
  table(ff$B14_2) 
  table(ff$B14_3) 
  table(ff$B14_4) 
  table(ff$B14_5) 
  table(ff$B14_6) 
  table(ff$B15_1) 
  table(ff$B15_2) 
  table(ff$B15_3) 
  table(ff$C17_1) 
  table(ff$C17_2) 
  table(ff$C17_3) 
  table(ff$C17_4)


table(ff$info_evo,ff$info_evo_b)
table(ff$info_evo>0)

table(ff$B14_1_evo,useNA = "ifany")

gg <- merge(ee,ff[,c("i001","i460","info_03","info_evo","info_evo_b","info_j","eff_IP","va","CAL","firm","poids_cal2",
                     "B11_1", 
                     "B11_2", 
                     "B11_3", 
                     "B11_4", 
                     "B11_5", 
                     "B11_6", 
                     "B11_7", 
                     "B12_1", 
                     "B12_2", 
                     "B12_3", 
                     "B12_4", 
                     "B13_1", 
                     "B13_2", 
                     "B14_1", 
                     "B14_2", 
                     "B14_3", 
                     "B14_4", 
                     "B14_5", 
                     "B14_6", 
                     "B15_1", 
                     "B15_2", 
                     "B15_3", 
                     "C17_1", 
                     "C17_2", 
                     "C17_3", 
                     "C17_4",
                     "E24_1",
                     "E24_2",
                     "E24_3",
                     "G32_1")]
            ,by="firm")
gc()


gg$itc_inv <- (gg$i460>0)*1
gg$itc_inv_work <- ifelse(gg$eff_IP>0,(gg$i460/gg$eff_IP),NA)
gg$itc_inv_rate <- ifelse(gg$CAL>0,(gg$i460)/gg$CAL,NA)


gg$f9010xf9010 <- ifelse(gg$f9010>0,(gg$f9010-1)/(gg$count-1),NA)
gg$f9910xf9910 <- ifelse(gg$f9910>0,(gg$f9910-1)/(gg$count-1),NA)
gg$one <- 1

gg$nb_esty <- ave(gg$one,paste(gg$firm,gg$year),FUN=sum)

gg$poids_final_est <- gg$poids_cal2/ifelse(is.na(gg$year)==T,1,gg$nb_esty)

gg$sf9010 <- ave(gg$f9010,gg$year,FUN=sum)
gg$f9010_w <- gg$f9010/gg$sf9010

gg$sf9910 <- ave(gg$f9910,gg$year,FUN=sum)
gg$f9910_w <- gg$f9910/gg$sf9910
gg$lnbwkrs <- ifelse(gg$count>0,log(gg$count),NA)

gg$f9010_w2 <-gg$poids_final_est*(gg$f9010/gg$count)
gg$f9910_w2 <-gg$poids_final_est*(gg$f9910/gg$count)

gg$count_firm <- ave(gg$count,paste(gg$firm,gg$year),FUN=function(x) sum(x,na.rm=T))
gc()

gg$min_year <- ave(gg$year,gg$est,FUN=min)
gg$max_year <- ave(gg$year,gg$est,FUN=max)


gg_b <- gg[,c("est","year","count","count_firm")]
gg_b$year <- gg_b$year + 1
colnames(gg_b)<- c("est","year","lcount","lcount_firm")
gg <- merge(gg,gg_b,by=c("est","year"),all.x=T)
rm(gg_b)
gg <- gg[order(gg$est,gg$year),]

gc()
gg$lndnbwkrs_neg <- (log(gg$count)-log(gg$lcount))*((log(gg$count)-log(gg$lcount))<=0)
gg$lndnbwkrs_neg <- ifelse(is.na(gg$lndnbwkrs_neg),log(gg$count),gg$lndnbwkrs_neg)
gg$lnnbwkrs_cumneg <- ave(gg$lndnbwkrs_neg,gg$est,FUN=function(x) cumsum(x))



#----------------------------------------------------#
#### Models ####
#----------------------------------------------------#
gc()
prop.table(table(gg$info_evo_b>0,gg$itc_inv),1)
cor.test(1*(gg$info_evo_b>1),1*(gg$itc_inv>0))
gg$digitalization <- (gg$info_evo_b>0)*1
# gg$digitalization <- (gg$info_evo>0)*1

table(gg$digitalization)

gg$digitalization_b <- (gg$info_evo_b + gg$G32_1 >0)*1
# gg$digitalization_b <- (gg$info_evo + gg$G32_1 >0)*1
table(gg$digitalization_b)



field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization*(year==2000))
                + I(digitalization*(year==2001))
                + I(digitalization*(year==2002))
                # + I(digitalization*(year==2003))
                + I(digitalization*(year==2004))
                + I(digitalization*(year==2005))
                + I(digitalization*(year==2006))
                + I(digitalization*(year==2007))
                + I(digitalization*(year==2008))
                + I(digitalization*(year==2009))
                # + I(digitalization*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm90_12 <- felm(I(100*f9010xf9010) ~ factor(year) 
                  + I(digitalization*(year==2000))
                  + I(digitalization*(year==2001))
                  + I(digitalization*(year==2002))
                  # + I(digitalization*(year==2003))
                  + I(digitalization*(year==2004))
                  + I(digitalization*(year==2005))
                  + I(digitalization*(year==2006))
                  + I(digitalization*(year==2007))
                  + I(digitalization*(year==2008))
                  + I(digitalization*(year==2009))
                  # + I(digitalization*(year==2010))
                  + lnbwkrs
                  + lnnbwkrs_cumneg
                  |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm90_11,mm90_12),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm90_11,mm90_12),stars=c(0.1,0.05,0.01,0.001),file="digit1.html")



field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization_b*(year==2000))
                + I(digitalization_b*(year==2001))
                + I(digitalization_b*(year==2002))
                # + I(digitalization_b*(year==2003))
                + I(digitalization_b*(year==2004))
                + I(digitalization_b*(year==2005))
                + I(digitalization_b*(year==2006))
                + I(digitalization_b*(year==2007))
                + I(digitalization_b*(year==2008))
                + I(digitalization_b*(year==2009))
                # + I(digitalization_b*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm90_12 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization_b*(year==2000))
                + I(digitalization_b*(year==2001))
                + I(digitalization_b*(year==2002))
                # + I(digitalization_b*(year==2003))
                + I(digitalization_b*(year==2004))
                + I(digitalization_b*(year==2005))
                + I(digitalization_b*(year==2006))
                + I(digitalization_b*(year==2007))
                + I(digitalization_b*(year==2008))
                + I(digitalization_b*(year==2009))
                # + I(digitalization_b*(year==2010))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm90_11,mm90_12),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm90_11,mm90_12),stars=c(0.1,0.05,0.01,0.001),file="digit1_b.html")




field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_21 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I((itc_inv)*(year==2000))
                + I((itc_inv)*(year==2001))
                + I((itc_inv)*(year==2002))
                # + I((itc_inv)*(year==2003))
                + I((itc_inv)*(year==2004))
                + I((itc_inv)*(year==2005))
                + I((itc_inv)*(year==2006))
                + I((itc_inv)*(year==2007))
                + I((itc_inv)*(year==2008))
                + I((itc_inv)*(year==2009))
                # + I((itc_inv)*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm90_22 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I((itc_inv)*(year==2000))
                + I((itc_inv)*(year==2001))
                + I((itc_inv)*(year==2002))
                # + I((itc_inv)*(year==2003))
                + I((itc_inv)*(year==2004))
                + I((itc_inv)*(year==2005))
                + I((itc_inv)*(year==2006))
                + I((itc_inv)*(year==2007))
                + I((itc_inv)*(year==2008))
                + I((itc_inv)*(year==2009))
                # + I((itc_inv)*(year==2010))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm90_21,mm90_22),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm90_21,mm90_22),stars=c(0.1,0.05,0.01,0.001),file="digit2.html")



field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% c(2003,2007)  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm290_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                # + I(digitalization*(year==2000))
                # + I(digitalization*(year==2001))
                # + I(digitalization*(year==2002))
                # + I(digitalization*(year==2003))
                # + I(digitalization*(year==2004))
                # + I(digitalization*(year==2005))
                # + I(digitalization*(year==2006))
                + I(digitalization*(year==2007))
                # + I(digitalization*(year==2008))
                # + I(digitalization*(year==2009))
                # + I(digitalization*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm290_12 <- felm(I(100*f9010xf9010) ~ factor(year) 
                # + I(digitalization*(year==2000))
                # + I(digitalization*(year==2001))
                # + I(digitalization*(year==2002))
                # + I(digitalization*(year==2003))
                # + I(digitalization*(year==2004))
                # + I(digitalization*(year==2005))
                # + I(digitalization*(year==2006))
                + I(digitalization*(year==2007))
                # + I(digitalization*(year==2008))
                # + I(digitalization*(year==2009))
                # + I(digitalization*(year==2010))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm290_11,mm290_12),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm290_11,mm290_12),stars=c(0.1,0.05,0.01,0.001),file="digit3.html")



field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% c(2003,2007)  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm290_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                 # + I(digitalization_b*(year==2000))
                 # + I(digitalization_b*(year==2001))
                 # + I(digitalization_b*(year==2002))
                 # + I(digitalization_b*(year==2003))
                 # + I(digitalization_b*(year==2004))
                 # + I(digitalization_b*(year==2005))
                 # + I(digitalization_b*(year==2006))
                 + I(digitalization_b*(year==2007))
                 # + I(digitalization_b*(year==2008))
                 # + I(digitalization_b*(year==2009))
                 # + I(digitalization_b*(year==2010))
                 |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm290_12 <- felm(I(100*f9010xf9010) ~ factor(year) 
                 # + I(digitalization_b*(year==2000))
                 # + I(digitalization_b*(year==2001))
                 # + I(digitalization_b*(year==2002))
                 # + I(digitalization_b*(year==2003))
                 # + I(digitalization_b*(year==2004))
                 # + I(digitalization_b*(year==2005))
                 # + I(digitalization_b*(year==2006))
                 + I(digitalization_b*(year==2007))
                 # + I(digitalization_b*(year==2008))
                 # + I(digitalization_b*(year==2009))
                 # + I(digitalization_b*(year==2010))
                 + lnbwkrs
                 + lnnbwkrs_cumneg
                 |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm290_11,mm290_12),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm290_11,mm290_12),stars=c(0.1,0.05,0.01,0.001),file="digit3_b.html")


mm290_21 <- felm(I(100*f9010xf9010) ~ factor(year) 
                # + I((itc_inv)*(year==2000))
                # + I((itc_inv)*(year==2001))
                # + I((itc_inv)*(year==2002))
                # + I((itc_inv)*(year==2003))
                # + I((itc_inv)*(year==2004))
                # + I((itc_inv)*(year==2005))
                # + I((itc_inv)*(year==2006))
                + I((itc_inv)*(year==2007))
                # + I((itc_inv)*(year==2008))
                # + I((itc_inv)*(year==2009))
                # + I((itc_inv)*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])


mm290_22 <- felm(I(100*f9010xf9010) ~ factor(year) 
                # + I((itc_inv)*(year==2000))
                # + I((itc_inv)*(year==2001))
                # + I((itc_inv)*(year==2002))
                # + I((itc_inv)*(year==2003))
                # + I((itc_inv)*(year==2004))
                # + I((itc_inv)*(year==2005))
                # + I((itc_inv)*(year==2006))
                + I((itc_inv)*(year==2007))
                # + I((itc_inv)*(year==2008))
                 # + I((itc_inv)*(year==2009))
                # + I((itc_inv)*(year==2010))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(list(mm290_21,mm290_22),stars=c(0.1,0.05,0.01,0.001))
htmlreg(list(mm290_21,mm290_22),stars=c(0.1,0.05,0.01,0.001),file="digit4.html")


summary(mm290_11)

field_des <- gg$f9010xf9010>0 & is.na(gg$f9010_w2)==F & gg$min_year<=2003   & gg$max_year>=2007  & gg$year %in% 2006
d1 <- wtd.summary(gg$f9010xf9010[field_des],w=gg$f9010_w2[field_des])
d2 <- wtd.summary(gg$digitalization[field_des],w=gg$f9010_w2[field_des])
d3 <- wtd.summary(gg$digitalization_b[field_des],w=gg$f9010_w2[field_des])
d4 <- wtd.summary(gg$lnbwkrs[field_des],w=gg$f9010_w2[field_des])
d5 <- wtd.summary(gg$lnnbwkrs_cumneg[field_des],w=gg$f9010_w2[field_des])

des_match_coi <- rbind(d1,d2,d3,d4,d5) 
rownames(des_match_coi) <- NULL
write.csv(des_match_coi,"descript_coi.csv")


table(ff$info_evo_b)


#------------------------------------------------------#
#### More models ####
#------------------------------------------------------#
# Summary
# itc_inv_work does not work
# itc_inv_rate event worse and with crazy estimates.

table(ff$B11_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(B11_1*(year==2000))
                + I(B11_1*(year==2001))
                + I(B11_1*(year==2002))
                # + I(B11_1*(year==2003))
                + I(B11_1*(year==2004))
                + I(B11_1*(year==2005))
                + I(B11_1*(year==2006))
                + I(B11_1*(year==2007))
                + I(B11_1*(year==2008))
                + I(B11_1*(year==2009))
                # + I(B11_1*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$B11_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_2*(year==2000))
               + I(B11_2*(year==2001))
               + I(B11_2*(year==2002))
               # + I(B11_2*(year==2003))
               + I(B11_2*(year==2004))
               + I(B11_2*(year==2005))
               + I(B11_2*(year==2006))
               + I(B11_2*(year==2007))
               + I(B11_2*(year==2008))
               + I(B11_2*(year==2009))
               # + I(B11_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$B11_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_3*(year==2000))
               + I(B11_3*(year==2001))
               + I(B11_3*(year==2002))
               # + I(B11_3*(year==2003))
               + I(B11_3*(year==2004))
               + I(B11_3*(year==2005))
               + I(B11_3*(year==2006))
               + I(B11_3*(year==2007))
               + I(B11_3*(year==2008))
               + I(B11_3*(year==2009))
               # + I(B11_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_3),stars=c(0.1,0.05,0.01,0.001))




table(ff$B11_4)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_4 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_4*(year==2000))
               + I(B11_4*(year==2001))
               + I(B11_4*(year==2002))
               # + I(B11_4*(year==2003))
               + I(B11_4*(year==2004))
               + I(B11_4*(year==2005))
               + I(B11_4*(year==2006))
               + I(B11_4*(year==2007))
               + I(B11_4*(year==2008))
               + I(B11_4*(year==2009))
               # + I(B11_4*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_4),stars=c(0.1,0.05,0.01,0.001))




table(ff$B11_5)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_5 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_5*(year==2000))
               + I(B11_5*(year==2001))
               + I(B11_5*(year==2002))
               # + I(B11_5*(year==2003))
               + I(B11_5*(year==2004))
               + I(B11_5*(year==2005))
               + I(B11_5*(year==2006))
               + I(B11_5*(year==2007))
               + I(B11_5*(year==2008))
               + I(B11_5*(year==2009))
               # + I(B11_5*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_5),stars=c(0.1,0.05,0.01,0.001))




table(ff$B11_6)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_6 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_6*(year==2000))
               + I(B11_6*(year==2001))
               + I(B11_6*(year==2002))
               # + I(B11_6*(year==2003))
               + I(B11_6*(year==2004))
               + I(B11_6*(year==2005))
               + I(B11_6*(year==2006))
               + I(B11_6*(year==2007))
               + I(B11_6*(year==2008))
               + I(B11_6*(year==2009))
               # + I(B11_6*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_6),stars=c(0.1,0.05,0.01,0.001))




table(ff$B11_7)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm11_7 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B11_7*(year==2000))
               + I(B11_7*(year==2001))
               + I(B11_7*(year==2002))
               # + I(B11_7*(year==2003))
               + I(B11_7*(year==2004))
               + I(B11_7*(year==2005))
               + I(B11_7*(year==2006))
               + I(B11_7*(year==2007))
               + I(B11_7*(year==2008))
               + I(B11_7*(year==2009))
               # + I(B11_7*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm11_7),stars=c(0.1,0.05,0.01,0.001))
# EDI impacts


#
table(ff$B12_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm12_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B12_1*(year==2000))
               + I(B12_1*(year==2001))
               + I(B12_1*(year==2002))
               # + I(B12_1*(year==2003))
               + I(B12_1*(year==2004))
               + I(B12_1*(year==2005))
               + I(B12_1*(year==2006))
               + I(B12_1*(year==2007))
               + I(B12_1*(year==2008))
               + I(B12_1*(year==2009))
               # + I(B12_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm12_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$B12_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm12_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B12_2*(year==2000))
               + I(B12_2*(year==2001))
               + I(B12_2*(year==2002))
               # + I(B12_2*(year==2003))
               + I(B12_2*(year==2004))
               + I(B12_2*(year==2005))
               + I(B12_2*(year==2006))
               + I(B12_2*(year==2007))
               + I(B12_2*(year==2008))
               + I(B12_2*(year==2009))
               # + I(B12_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm12_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$B12_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm12_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B12_3*(year==2000))
               + I(B12_3*(year==2001))
               + I(B12_3*(year==2002))
               # + I(B12_3*(year==2003))
               + I(B12_3*(year==2004))
               + I(B12_3*(year==2005))
               + I(B12_3*(year==2006))
               + I(B12_3*(year==2007))
               + I(B12_3*(year==2008))
               + I(B12_3*(year==2009))
               # + I(B12_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm12_3),stars=c(0.1,0.05,0.01,0.001))




table(ff$B12_4)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm12_4 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B12_4*(year==2000))
               + I(B12_4*(year==2001))
               + I(B12_4*(year==2002))
               # + I(B12_4*(year==2003))
               + I(B12_4*(year==2004))
               + I(B12_4*(year==2005))
               + I(B12_4*(year==2006))
               + I(B12_4*(year==2007))
               + I(B12_4*(year==2008))
               + I(B12_4*(year==2009))
               # + I(B12_4*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm12_4),stars=c(0.1,0.05,0.01,0.001))


table(ff$B13_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm13_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B13_1*(year==2000))
               + I(B13_1*(year==2001))
               + I(B13_1*(year==2002))
               # + I(B13_1*(year==2003))
               + I(B13_1*(year==2004))
               + I(B13_1*(year==2005))
               + I(B13_1*(year==2006))
               + I(B13_1*(year==2007))
               + I(B13_1*(year==2008))
               + I(B13_1*(year==2009))
               # + I(B13_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm13_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$B13_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm13_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B13_2*(year==2000))
               + I(B13_2*(year==2001))
               + I(B13_2*(year==2002))
               # + I(B13_2*(year==2003))
               + I(B13_2*(year==2004))
               + I(B13_2*(year==2005))
               + I(B13_2*(year==2006))
               + I(B13_2*(year==2007))
               + I(B13_2*(year==2008))
               + I(B13_2*(year==2009))
               # + I(B13_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm13_2),stars=c(0.1,0.05,0.01,0.001))



#
table(ff$B14_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_1*(year==2000))
               + I(B14_1*(year==2001))
               + I(B14_1*(year==2002))
               # + I(B14_1*(year==2003))
               + I(B14_1*(year==2004))
               + I(B14_1*(year==2005))
               + I(B14_1*(year==2006))
               + I(B14_1*(year==2007))
               + I(B14_1*(year==2008))
               + I(B14_1*(year==2009))
               # + I(B14_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$B14_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_2*(year==2000))
               + I(B14_2*(year==2001))
               + I(B14_2*(year==2002))
               # + I(B14_2*(year==2003))
               + I(B14_2*(year==2004))
               + I(B14_2*(year==2005))
               + I(B14_2*(year==2006))
               + I(B14_2*(year==2007))
               + I(B14_2*(year==2008))
               + I(B14_2*(year==2009))
               # + I(B14_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$B14_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_3*(year==2000))
               + I(B14_3*(year==2001))
               + I(B14_3*(year==2002))
               # + I(B14_3*(year==2003))
               + I(B14_3*(year==2004))
               + I(B14_3*(year==2005))
               + I(B14_3*(year==2006))
               + I(B14_3*(year==2007))
               + I(B14_3*(year==2008))
               + I(B14_3*(year==2009))
               # + I(B14_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_3),stars=c(0.1,0.05,0.01,0.001))




table(ff$B14_4)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_4 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_4*(year==2000))
               + I(B14_4*(year==2001))
               + I(B14_4*(year==2002))
               # + I(B14_4*(year==2003))
               + I(B14_4*(year==2004))
               + I(B14_4*(year==2005))
               + I(B14_4*(year==2006))
               + I(B14_4*(year==2007))
               + I(B14_4*(year==2008))
               + I(B14_4*(year==2009))
               # + I(B14_4*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_4),stars=c(0.1,0.05,0.01,0.001))




table(ff$B14_5)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_5 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_5*(year==2000))
               + I(B14_5*(year==2001))
               + I(B14_5*(year==2002))
               # + I(B14_5*(year==2003))
               + I(B14_5*(year==2004))
               + I(B14_5*(year==2005))
               + I(B14_5*(year==2006))
               + I(B14_5*(year==2007))
               + I(B14_5*(year==2008))
               + I(B14_5*(year==2009))
               # + I(B14_5*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_5),stars=c(0.1,0.05,0.01,0.001))




table(ff$B14_6)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm14_6 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B14_6*(year==2000))
               + I(B14_6*(year==2001))
               + I(B14_6*(year==2002))
               # + I(B14_6*(year==2003))
               + I(B14_6*(year==2004))
               + I(B14_6*(year==2005))
               + I(B14_6*(year==2006))
               + I(B14_6*(year==2007))
               + I(B14_6*(year==2008))
               + I(B14_6*(year==2009))
               # + I(B14_6*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm14_6),stars=c(0.1,0.05,0.01,0.001))

#

table(ff$B15_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm15_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B15_1*(year==2000))
               + I(B15_1*(year==2001))
               + I(B15_1*(year==2002))
               # + I(B15_1*(year==2003))
               + I(B15_1*(year==2004))
               + I(B15_1*(year==2005))
               + I(B15_1*(year==2006))
               + I(B15_1*(year==2007))
               + I(B15_1*(year==2008))
               + I(B15_1*(year==2009))
               # + I(B15_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm15_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$B15_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm15_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B15_2*(year==2000))
               + I(B15_2*(year==2001))
               + I(B15_2*(year==2002))
               # + I(B15_2*(year==2003))
               + I(B15_2*(year==2004))
               + I(B15_2*(year==2005))
               + I(B15_2*(year==2006))
               + I(B15_2*(year==2007))
               + I(B15_2*(year==2008))
               + I(B15_2*(year==2009))
               # + I(B15_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm15_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$B15_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm15_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(B15_3*(year==2000))
               + I(B15_3*(year==2001))
               + I(B15_3*(year==2002))
               # + I(B15_3*(year==2003))
               + I(B15_3*(year==2004))
               + I(B15_3*(year==2005))
               + I(B15_3*(year==2006))
               + I(B15_3*(year==2007))
               + I(B15_3*(year==2008))
               + I(B15_3*(year==2009))
               # + I(B15_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm15_3),stars=c(0.1,0.05,0.01,0.001))


#
table(ff$C17_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm17_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(C17_1*(year==2000))
               + I(C17_1*(year==2001))
               + I(C17_1*(year==2002))
               # + I(C17_1*(year==2003))
               + I(C17_1*(year==2004))
               + I(C17_1*(year==2005))
               + I(C17_1*(year==2006))
               + I(C17_1*(year==2007))
               + I(C17_1*(year==2008))
               + I(C17_1*(year==2009))
               # + I(C17_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm17_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$C17_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm17_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(C17_2*(year==2000))
               + I(C17_2*(year==2001))
               + I(C17_2*(year==2002))
               # + I(C17_2*(year==2003))
               + I(C17_2*(year==2004))
               + I(C17_2*(year==2005))
               + I(C17_2*(year==2006))
               + I(C17_2*(year==2007))
               + I(C17_2*(year==2008))
               + I(C17_2*(year==2009))
               # + I(C17_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm17_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$C17_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm17_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(C17_3*(year==2000))
               + I(C17_3*(year==2001))
               + I(C17_3*(year==2002))
               # + I(C17_3*(year==2003))
               + I(C17_3*(year==2004))
               + I(C17_3*(year==2005))
               + I(C17_3*(year==2006))
               + I(C17_3*(year==2007))
               + I(C17_3*(year==2008))
               + I(C17_3*(year==2009))
               # + I(C17_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm17_3),stars=c(0.1,0.05,0.01,0.001))




table(ff$C17_4)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm17_4 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(C17_4*(year==2000))
               + I(C17_4*(year==2001))
               + I(C17_4*(year==2002))
               # + I(C17_4*(year==2003))
               + I(C17_4*(year==2004))
               + I(C17_4*(year==2005))
               + I(C17_4*(year==2006))
               + I(C17_4*(year==2007))
               + I(C17_4*(year==2008))
               + I(C17_4*(year==2009))
               # + I(C17_4*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm17_4),stars=c(0.1,0.05,0.01,0.001))


#
table(ff$E24_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm24_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(E24_1*(year==2000))
               + I(E24_1*(year==2001))
               + I(E24_1*(year==2002))
               # + I(E24_1*(year==2003))
               + I(E24_1*(year==2004))
               + I(E24_1*(year==2005))
               + I(E24_1*(year==2006))
               + I(E24_1*(year==2007))
               + I(E24_1*(year==2008))
               + I(E24_1*(year==2009))
               # + I(E24_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm24_1),stars=c(0.1,0.05,0.01,0.001))

table(ff$E24_2)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm24_2 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(E24_2*(year==2000))
               + I(E24_2*(year==2001))
               + I(E24_2*(year==2002))
               # + I(E24_2*(year==2003))
               + I(E24_2*(year==2004))
               + I(E24_2*(year==2005))
               + I(E24_2*(year==2006))
               + I(E24_2*(year==2007))
               + I(E24_2*(year==2008))
               + I(E24_2*(year==2009))
               # + I(E24_2*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm24_2),stars=c(0.1,0.05,0.01,0.001))



table(ff$E24_3)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm24_3 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(E24_3*(year==2000))
               + I(E24_3*(year==2001))
               + I(E24_3*(year==2002))
               # + I(E24_3*(year==2003))
               + I(E24_3*(year==2004))
               + I(E24_3*(year==2005))
               + I(E24_3*(year==2006))
               + I(E24_3*(year==2007))
               + I(E24_3*(year==2008))
               + I(E24_3*(year==2009))
               # + I(E24_3*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm24_3),stars=c(0.1,0.05,0.01,0.001))


#
table(ff$G32_1)
field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm32_1 <- felm(I(100*f9010xf9010) ~ factor(year) 
               + I(G32_1*(year==2000))
               + I(G32_1*(year==2001))
               + I(G32_1*(year==2002))
               # + I(G32_1*(year==2003))
               + I(G32_1*(year==2004))
               + I(G32_1*(year==2005))
               + I(G32_1*(year==2006))
               + I(G32_1*(year==2007))
               + I(G32_1*(year==2008))
               + I(G32_1*(year==2009))
               # + I(G32_1*(year==2010))
               |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])

screenreg(list(mm32_1),stars=c(0.1,0.05,0.01,0.001))



field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization_b*(year==2000))
                + I(digitalization_b*(year==2001))
                + I(digitalization_b*(year==2002))
                # + I(digitalization_b*(year==2003))
                + I(digitalization_b*(year==2004))
                + I(digitalization_b*(year==2005))
                + I(digitalization_b*(year==2006))
                + I(digitalization_b*(year==2007))
                + I(digitalization_b*(year==2008))
                + I(digitalization_b*(year==2009))
                # + I(digitalization_b*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(mm90_11,stars=c(0.1,0.05,0.01))



gg$digitalization_c <- (gg$info_evo_b + gg$G32_1>0)*1

field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization_c*(year==2000))
                + I(digitalization_c*(year==2001))
                + I(digitalization_c*(year==2002))
                # + I(digitalization_c*(year==2003))
                + I(digitalization_c*(year==2004))
                + I(digitalization_c*(year==2005))
                + I(digitalization_c*(year==2006))
                + I(digitalization_c*(year==2007))
                + I(digitalization_c*(year==2008))
                + I(digitalization_c*(year==2009))
                # + I(digitalization_c*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(mm90_11,stars=c(0.1,0.05,0.01))



gg$digitalization_d <- (gg$info_evo + gg$G32_1 + gg$itc_inv >0 )*1
# gg$digitalization_b <- (gg$info_evo + gg$G32_1 >0)*1
table(gg$digitalization_d)


field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year<2010  & gg$min_year<=2003   & gg$max_year>=2007  # & gg$min_year==2000 & gg$max_year==2010
mm90_11 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(digitalization_d*(year==2000))
                + I(digitalization_d*(year==2001))
                + I(digitalization_d*(year==2002))
                # + I(digitalization_d*(year==2003))
                + I(digitalization_d*(year==2004))
                + I(digitalization_d*(year==2005))
                + I(digitalization_d*(year==2006))
                + I(digitalization_d*(year==2007))
                + I(digitalization_d*(year==2008))
                + I(digitalization_d*(year==2009))
                # + I(digitalization_d*(year==2010))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
screenreg(mm90_11,stars=c(0.1,0.05,0.01))

